package com.hyjx.framework.component.theme;

import com.alibaba.fastjson.JSON;
import com.hyjx.framework.service.ConfigManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;

/**
 * 主题控制器
 */
@Controller
@RequestMapping("/theme")
public class ThemeController {
    @Resource
    private ThemeService service;

    /**
     * 进入到信息查询页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoList", method = RequestMethod.GET)
    public String gotoList(HttpServletRequest request) {
        return "/Theme/list";
    }

    /**
     * 执行客户查询操作
     *
     * @param model
     * @param request
     * @return
     */
    @SuppressWarnings({"rawtypes", "deprecation", "unchecked"})
    @RequestMapping(value = "/queryList", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String queryList(ThemeModel model, HttpServletRequest request) {
        //String path = request.getRealPath("theme");
        String path = this.getClass().getClassLoader().getResource("").getPath() + "static/theme";
        java.io.File file = new java.io.File(path);
        File files[] = file.listFiles();
        ArrayList list = new ArrayList();
        for (File f : files) {
            if (f.isFile() && f.getName().toLowerCase().endsWith(".css")) {
                if ("2".equals(ConfigManager.getUiLayout()) && f.getName().toLowerCase().indexOf("layout") > -1L) {
                    list.add(f.getName().split("\\.")[0]);
                } else if ("1".equals(ConfigManager.getUiLayout()) && f.getName().indexOf("layout") == -1) {
                    list.add(f.getName().split("\\.")[0]);
                }
            }
        }
        return JSON.toJSONString(list);
    }

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoAdd", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoAdd(HttpServletRequest request) {
        return "/Theme/add";
    }

    /**
     * 增加信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    @ResponseBody
    public String doAdd(ThemeModel model) {
        if (service.add(model)) {
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 跳转到修改页面
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoEdit", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoEdit(HttpServletRequest request) {
        //String id = request.getParameter("id");
        //ThemeModel model = service.getInfoById(id);
        ModelAndView mav = new ModelAndView("/Theme/edit");
        //mav.addObject("model",model);
        return mav;
    }

    /**
     * 修改信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doEdit", method = RequestMethod.POST)
    @ResponseBody
    public String doEdit(ThemeModel model) {
//		if(service.edit(model)>0){
//			return "success";
//		}else{
//			return "failure";
//		}
        return null;
    }

    /**
     * 删除信息
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoDelete", method = RequestMethod.GET)
    @ResponseBody
    public String gotoDelete(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        if (service.delete(ids) > 0) {
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 信息查看
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoView", method = RequestMethod.GET)
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        ThemeModel model = service.getInfoById(id);
        ModelAndView mav = new ModelAndView("/Theme/view");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 保存主题
     *
     * @param request
     * @param response
     * @param theme
     * @return
     */
    @RequestMapping(value = "/savetheme", method = RequestMethod.POST)
    public String savetheme(HttpServletRequest request, HttpServletResponse response,
                            @CookieValue(value = "theme", defaultValue = "") String theme
    ) {
        String themeid = request.getParameter("id");
        Cookie cookie = new Cookie("theme", themeid);
        cookie.setPath("/");
        cookie.setMaxAge(60 * 60 * 24 * 365);
        response.addCookie(cookie);
        return null;
    }

}